layui.use(['table', 'admin', 'laydate','ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    /**
     * 门店退货登记管理
     */
    var HxStoreReturnsRegister = {
        tableId: "hxStoreReturnsRegisterTable"
    };

    /**
     * 初始化表格的列
     */
    HxStoreReturnsRegister.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'unitName', sort: true, title: '门店名称'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'refundNum', sort: true, title: '退货箱数'},
            {field: 'carryDate', sort: true, title: '提走日期'},
            {field: 'refundFeedback', sort: true, title: '退货反馈'},
            {field: 'refundPhotoId', hide: true, title: '退货照片id'},
            {align: 'center', sort: true, toolbar: '#tableBar4', title: '退货照片'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    HxStoreReturnsRegister.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['goodsName'] = $("#condition").val();
        queryData['unitName'] = $("#condition1").val();
        queryData['operateId'] = $("#operateId").val();
        table.reload(HxStoreReturnsRegister.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    HxStoreReturnsRegister.exportExcel = function () {
        var checkRows = table.checkStatus(HxStoreReturnsRegister.tableId);

        var originalColumns = HxStoreReturnsRegister.initColumn()[0];
        var dataToExport = checkRows.data;
        // 移除数据
        dataToExport = dataToExport.map(function (row){
            delete row.id;
            delete row.center;
            return row;
        })
        // 移除表头
        originalColumns = originalColumns.filter(column => {
            return column.field !== 'id' && column.field !== 'center';
        })

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            exportToXlsx(dataToExport, originalColumns, '门店退货登记统计.xlsx')
            //table.exportFile( tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxStoreReturnsRegister.tableId,
        url: Feng.ctxPath + '/hxStoreReturnsRegister/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: HxStoreReturnsRegister.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStoreReturnsRegister.search();
    });
    
    // 导出excel
    $('#btnExp').click(function () {
        HxStoreReturnsRegister.exportExcel();
    });

    // 单击行事件
    table.on('row(' + HxStoreReturnsRegister.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + HxStoreReturnsRegister.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + HxStoreReturnsRegister.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    // 工具条点击事件
    table.on('tool(' + HxStoreReturnsRegister.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'paymentProve') {
            //构建图像数组
            var items = [];
            if (data.refundPhotoId!='') {
                var obj = {};
                obj.src = '/system/previewa/' + data.refundPhotoId;
                obj.title = '预览图片';
                items.push(obj)
                //定义选项（如果需要）
                var options = {
                    index: 0 // 此选项表示您将从第一张图片开始
                };
                //初始化插件
                var viewer = new PhotoViewer(items, options);
            } else {
                Feng.info("暂无照片!");
            }
        }

    });

    $('#weChat').click(function () {
        layer.open({
            type: 2,
            title: '门店退货登记',
            area: ['1000px', '550px'],
            anim: 5,
            content: Feng.ctxPath + '/hxStoreReturnsRegister/toqrCode',
            end: function () {
            }
        });
    });
});
